//
//  PreviewViewController.swift
//  PDF2PNG
//
//  Created by Jarvis on 08/05/2017.
//  Copyright © 2017 Jarvis. All rights reserved.
//

import UIKit
import MBProgressHUD

class PreviewViewController: ViewController {

    var previewImage: UIImage?
    
    override func viewDidLoad() {
        super.viewDidLoad()

        self.view.backgroundColor = UIColor.white;
        
        self.title = "Preview".localized()
        let rightItem = UIBarButtonItem(barButtonSystemItem: .action, target: self, action: #selector(rightItemPressed(_:)))
        self.navigationItem.rightBarButtonItem = rightItem
        
        
        let scrollView = UIScrollView()
        self.view.addSubview(scrollView)
        scrollView.frame = self.view.bounds
        
        
        let imageView = UIImageView()
        scrollView.addSubview(imageView)
        imageView.contentMode = .scaleAspectFit
        
        if let image = previewImage {
            imageView.image = image;
            
            let width = self.view.bounds.size.width
            let height = width / (image.size.width / image.size.height)
            
            imageView.frame = CGRect(x: 0, y: 0, width: width, height: height)
            
            scrollView.contentSize = CGSize(width: width, height: height);
            
            // 显示图片尺寸信息
            let infoLabel = UILabel()
            self.view.addSubview(infoLabel)
            infoLabel.text = String(format: "%.0f * %.0f", width, height)
            infoLabel.textColor = UIColor.black.withAlphaComponent(0.6)
            infoLabel.font = UIFont.systemFont(ofSize: 10)
            infoLabel.snp.makeConstraints { (make) in
                make.right.equalTo(view.snp.right).offset(-10)
                make.bottom.equalTo(view.snp.bottom).offset(-10)
            }
        }
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    @objc func rightItemPressed(_ buttonItem: UIBarButtonItem) {
        MBProgressHUD.showAdded(to: UIApplication.shared.keyWindow!, animated: true)
        
        if let jpgImage = UIImageJPEGRepresentation(previewImage!, 0.9) {
//            let shareMessage = "Hey! Here is an interesting photo. #Tag";
//            var shareItems = [shareMessage, jpgImage]
            
            let imageWidth = Int64(previewImage!.size.width)
            let imageHeight = Int64(previewImage!.size.height)
            let imageDataSize = Int64(jpgImage.count)
            print("ImageInfo: \(imageWidth), \(imageHeight), \(imageDataSize)")
            Tracker.shareMergedImage(imageWidth: imageWidth, imageHeight: imageHeight, dataSize: imageDataSize)
            
            let shareItems = [jpgImage]
            
            let activity = UIActivityViewController(activityItems: shareItems, applicationActivities: nil)
            
            // iPad使用popover弹窗
            if UIDevice.current.userInterfaceIdiom == .pad {
                if activity.responds(to: #selector(getter: popoverPresentationController)) {
                    activity.popoverPresentationController?.sourceView = self.view
                    activity.popoverPresentationController?.barButtonItem = self.navigationItem.rightBarButtonItem
                }
            }
            
            self.present(activity, animated: true, completion: { 
                MBProgressHUD.hide(for: UIApplication.shared.keyWindow!, animated: true)
            })
            
            return;
        }else {
            MBProgressHUD.hide(for: UIApplication.shared.keyWindow!, animated: true)
        }
    }

}
